package com.zh.tea.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.CartMapper;
import com.zh.tea.entity.Cart;
import com.zh.tea.entity.CartExample;
import com.zh.tea.service.CartService;
import com.zh.tea.utils.SessionFactoryUtils;

public class CartServiceImpl implements CartService {
/**
 * 购物车添加
 */
	@Override
	public int addCart(Cart cart) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		CartMapper cm=session.getMapper(CartMapper.class);
		int a = cm.insert(cart);
		session.commit();
		session.close();
		return a;
	}
/**
 * 购物车删除
 */
	@Override
	public int delCart(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		CartMapper cm=session.getMapper(CartMapper.class);
		int a = cm.deleteByPrimaryKey(id);
		session.commit();
		session.close();
		return a;
	}
/**
 * 购物车更新
 */
	@Override
	public int updateCart(Cart Cart) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		CartMapper cm=session.getMapper(CartMapper.class);
		int a = cm.updateByPrimaryKey(Cart);
		session.commit();
		session.close();
		return a;
	}
	/**
	 * 购物车所有信息
	 */
	@Override
	public List<Cart> getAll() {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		CartMapper cm=session.getMapper(CartMapper.class);
		CartExample ce = new CartExample();
		CartExample.Criteria criterianame=ce.createCriteria();
		List<Cart> list=cm.selectByExample(ce);
		return list;
	}
	/**
	 * 查询所有分页，如需按条件查询，可增加if
	 */
	@Override
	public PageInfo<Cart> getCartByPage(Cart cart, Integer pagNo, Integer PageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		CartMapper cm=session.getMapper(CartMapper.class);
		CartExample ce = new CartExample();
		CartExample.Criteria criterianame=ce.createCriteria();
		if(cart.getId()!=null){
			criterianame.andIdEqualTo(cart.getId());
		}
		PageHelper.startPage(pagNo, PageSize);
		List<Cart> list=cm.selectByExample(ce);
		PageInfo<Cart> page=new PageInfo<>(list);
		return page;
	}

}
